﻿@{
    ViewBag.Title = "Ins";
}
@section scripts
{   
    <script src="@Url.Content("~/Scripts/shared/share.js")" type="text/javascript"></script> 
    <script src="@Url.Content("~/Scripts/shared/shown.js")" type="text/javascript"></script>  
    <script src="@Url.Content("~/Scripts/common/gridlayout.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/common/export.js")" type="text/javascript"></script>
}
<body>
   <form id="form1" runat="server" method="post" style="height: 100%">
    
    <div class="mini-toolbar" style="padding:2px;border-top:0;border-left:0;border-right:0;"> 
        <table style="width:100%;">
            <tr>
                <td style="width:100%;">
                    <a class="mini-button" id="refresh" iconCls="icon-search" onclick="search" plain="true">刷新</a>                    
                    <a class="mini-button" id="check" name="check" iconCls="icon-ok" onclick="checkBill" plain="true" visible="@ViewBag.checkend">盘点完成</a>                                        
                    <span class="separator"></span>
                    <a class="mini-button" id="close" iconCls="icon-close" onclick="CloseWindow" plain="true">关闭</a>
                </td>
            </tr>
        </table>                
    </div>
    
    <div id = "DivHeader">
        <table>
            <tr>
                <td width ="70px" align="right">门店名称：</td>
                <td width ="200px">
                    @Html.Partial("~/Views/Modular/LookupStockInfo.cshtml")                    
                </td>    
                <td width="80px"align="right">漏盘方式：</td>
                <td width="160px">
                    <input id="FDataType" name="FDataType" class="mini-combobox" style="width:150px;margin-left:0px;" showNullItem="true"
                            allowInput="false" align="left" emptyText="请选择..." nullItemText="请选择..." data="comDataType" />
                </td>                            
                <td width ="280px">提示：必须选择一个门店和漏盘方式进行完成盘点。</td>
            </tr>
        </table>
    </div>
    <div class="mini-fit">
        <div id="datagrid1" class="mini-datagrid" style="width:100%;height:100%;" borderStyle="border:1;"pageSize="15"
            url="../Report/GetBillList"
            allowCellSelect="true" allowCellEdit="true" showSummaryRow="true" ondrawsummarycell="onDrawSummaryCell"
            fitColumns="false" onrowdblclick="editBill" sortMode="client">
            <div property="columns">
                <div type="checkcolumn" ></div>                           
                <div field="FBillNo" width="120" headerAlign="center">单据编号</div>
                <div field="FDate" name="FDate" width="90px" dateFormat="yyyy-MM-dd" headerAlign="center">单据日期</div> 
                <div field="FQty_St" name="FQty_St" width="100" headerAlign="center"align="right"numberFormat="#,0.00">结存数量</div>
                <div field="FQty" name="FQty" width="100" headerAlign="center"align="right"numberFormat="#,0.00">盘点数量</div>
                <div field="FAmount" name="FAmount" width="120" headerAlign="center"align="right"numberFormat="#,0.00">金额</div>
                <div field="FStockName" width="120" headerAlign="center">门店名称</div>
                <div field="FUserName" width="90" headerAlign="center">用户名称</div> 
            </div>
        </div>  
    </div>
    
    <script type="text/javascript">

        var comDataType = [
            { "id": "2", "text": "漏盘商品库存归零" },
            { "id": "3", "text": "漏盘商品库存不变" }
        ];

        mini.parse();
        var form = new mini.Form("form1");
        var grid = mini.get("datagrid1");
        var uID = parent.window.getSystemAppID();
        var hdatatype = getQueryString("datatype");
        var sumFields = "FQty_St,FQty,FAmount";

        function FormShown() {
            LoadData();
        }

        function LoadData() {
            $.ajax({
                url: '../Bill/GetBillData',
                data: { mSystemAppID: uID, mBillID: "0", mDataType: "4"},
                dataType: 'text',
                async: false,
                success: function (text) {
                    var dataR = mini.decode(text);
                    if (dataR.datamaster.length > 0) {
                        var mdata = dataR.datamaster[0];
                        mini.getbyName("FStockID").setValue(mdata.FStockID);
                        mini.getbyName("FStockID").setText(mdata.FStockNumber);
                        mini.getbyName("FStockName").setValue(mdata.FStockName);
                        search();
                    }
                }
            });
        }

        function checkBill() {
            mini.getbyName("check").setEnabled(false);
            var mStockID = mini.getbyName("FStockID").getValue();
            var mDataType = mini.getbyName("FDataType").getValue();
            if ((mStockID == "") || (mStockID == "0")) {
                mini.getbyName("check").setEnabled(true);
                mini.alert("请选择一个门店进行完成盘点。");
                return;
            }
            if ((mDataType == "") || (mDataType == "0")) {
                mini.getbyName("check").setEnabled(true);
                mini.alert("请选择一个漏税盘方式再完成盘点。");
                return;
            }
            mini.confirm("确定要完成盘点码？", "提示",
                function (action) {
                    if (action == "ok") {
                        setCheckBill();
                    }
                });
        }

        function setCheckBill() {
            var mStockID = mini.getbyName("FStockID").getValue();
            var mDataType = mini.getbyName("FDataType").getValue();
            var rCnt = grid.getData().length;           

            var mjson = "[{'FSystemAppID':'" + uID + "',";
            mjson = mjson + "'FStockID':'" + mStockID + "',";
            mjson = mjson + "'FDataType':'" + mDataType + "',";
            mjson = mjson + "'bill_list':";

            var js = "[";
            for (var i = 0; i < rCnt; i++) {
                var rs = grid.getRow(i);
                if (i > 0) js = js + ",";
                js = js + '{"KInterID":' + rs.KInterID + "}";
            }

            if (js.length < 6) {
                mini.getbyName("check").setEnabled(true);
                mini.alert("完成盘点失败,没有要完成盘点的盘点数据!");
                return;
            }
            js = js + "]";
            mjson = mjson + js + "}]";
            $.ajax({
                url: '../Bill/CheckStockBill',
                data: { mdatajson: mjson },
                type: "post",
                cache: false,
                success: function (txt) {
                    var ret = mini.decode(txt);
                    if (ret.id != -1) {
                        mini.alert("完成盘点成功.");
                        search();
                    }
                    else {
                        mini.getbyName("check").setEnabled(true);
                        mini.alert("异常:" + ret.Status);
                    }
                },
                error: function (xhr) {
                    mini.getbyName("check").setEnabled(true);
                    mini.alert("异常:" + xhr.responseText);
                }
            });
        }

        function editBill() {
            var row = grid.getSelected();
            if (row) {
                parent.window.showTabByIdEx('57', 'StcData', '库存盘点', '../Bill/StcData?datatype=4&billid=' + row.KInterID);
            }
        }

        function search() {
            var cDate1 = "1901-01-01";
            var cDate2 = "2099-01-01";
            var mCheckFlag = "1";
            var mContent = "";
            var mBillNo = "";
            var mStockID = mini.getbyName("FStockID").getValue();
            var mRemark = "";

            var json = "[{";
            json = json + '"mListType":' + '"4",';//列表类型
            json = json + '"mSystemAppID":' + '"' + uID + '",';
            json = json + '"mDate1":' + '"' + cDate1 + '",';
            json = json + '"mDate2":' + '"' + cDate2 + '",';
            json = json + '"mCheckFlag":' + '"' + mCheckFlag + '",';
            json = json + '"mContent":' + '"' + mContent + '",';
            json = json + '"mStockID":' + '"' + mStockID + '",';
            json = json + '"mRemark":' + '"' + mRemark + '",';
            json = json + '"mBillNo":' + '"' + mBillNo + '"';
            json = json + "}]";

            grid.load({ mydata: json });
            mini.getbyName("check").setEnabled(true);
            mExportDataUrl = grid.url;
            mExportDataJson = json;//导出
        }

        function printdata() {
            var url = "../Report/PrintReport";
            mini.open({
                url: url,
                title: "打印", width: 1000, height: 700,
                onload: function () {
                    var iframe = this.getIFrameEl();
                    var NoneDisplayCol = [0];
                    iframe.contentWindow.InitalPrint(grid, NoneDisplayCol, parent.window.getSystemCompany() + "库存盘点单列表");
                }
            });
        }

    </script>

    </form>
</body>